<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/logo1.jpg?v=7.1.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/logo1.jpg?v=7.1.0">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.0" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.0',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="C++11标准发布后，C++几乎变成了一门全新的语言，花了两周看了C++11的特性，做了以下一个简单的归纳，并不完全包含所有C++11特性。">
<meta name="keywords" content="C++11">
<meta property="og:type" content="article">
<meta property="og:title" content="C++11新特性总结">
<meta property="og:url" content="https://luoo.oschina.io/2016/08/12/tech/C++11新特性/index.html">
<meta property="og:site_name" content="从你的全世界路过">
<meta property="og:description" content="C++11标准发布后，C++几乎变成了一门全新的语言，花了两周看了C++11的特性，做了以下一个简单的归纳，并不完全包含所有C++11特性。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-14T12:28:00.705Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="C++11新特性总结">
<meta name="twitter:description" content="C++11标准发布后，C++几乎变成了一门全新的语言，花了两周看了C++11的特性，做了以下一个简单的归纳，并不完全包含所有C++11特性。">



  <link rel="alternate" href="/atom.xml" title="从你的全世界路过" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://luoo.oschina.io/2016/08/12/tech/C++11新特性/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>C++11新特性总结 | 从你的全世界路过</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">从你的全世界路过</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">心智渐开，世事渐杂</p>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-schedule">

    
    
    
      
    

    

    <a href="/schedule/" rel="section"><i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>计划</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-you">

    
    
    
      
    

    

    <a href="/you/" rel="section"><i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>知遇</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://luoo.oschina.io/2016/08/12/tech/C++11新特性/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="方林">
      <meta itemprop="description" content="愿你出走半生，归来仍是少年">
      <meta itemprop="image" content="/images/logo1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="从你的全世界路过">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">C++11新特性总结

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2016-08-12 23:20:25" itemprop="dateCreated datePublished" datetime="2016-08-12T23:20:25+08:00">2016-08-12</time>
            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/编程技术/" itemprop="url" rel="index"><span itemprop="name">编程技术</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/2016/08/12/tech/C++11新特性/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2016/08/12/tech/C++11新特性/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>C++11标准发布后，C++几乎变成了一门全新的语言，花了两周看了C++11的特性，做了以下一个简单的归纳，并不完全包含所有C++11特性。</p>
<a id="more"></a>
<h1 id="新特性列表"><a href="#新特性列表" class="headerlink" title="新特性列表"></a><strong>新特性列表</strong></h1><ol>
<li>auto</li>
<li>prevention of narrowing</li>
<li>constexpr</li>
<li>using</li>
<li>nullptr</li>
<li>enum class</li>
<li>static_assert</li>
<li>raw string literals</li>
<li>delegating constructor</li>
<li>in-class member initializers</li>
<li>variadic templates</li>
<li>user-defined literals</li>
<li>override and final</li>
<li>explicit conversion operators</li>
<li>default and delete</li>
<li>decltype and declval</li>
<li>suffix return type syntax</li>
<li>range based for loop</li>
<li>std::function and std::bind</li>
<li>tuple and tie</li>
<li>STL</li>
<li><strong>type traits</strong></li>
<li><strong>lambda and closures</strong></li>
<li><strong>uniform and general initialization</strong></li>
<li><strong>memory management</strong></li>
<li><strong>rvalue references (move semantics)</strong></li>
<li><strong>threads and memory model</strong></li>
</ol>
<h1 id="auto"><a href="#auto" class="headerlink" title="auto"></a><strong>auto</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">auto p = new T();</span><br><span class="line">auto &amp;s = *p;</span><br></pre></td></tr></table></figure>
<p>这个东西类似C#中的var，不过区别在于当要定义引用类型的时候，不要忘了加&amp;</p>
<h1 id="prevention-of-narrowing"><a href="#prevention-of-narrowing" class="headerlink" title="prevention of narrowing"></a><strong>prevention of narrowing</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int x = 6.3; //warning!</span><br><span class="line">int z = &#123;6.3&#125;; //error: narrowing</span><br></pre></td></tr></table></figure>
<p>C++11新加了类型转换检查，以避免潜在的错误。</p>
<h1 id="constexpr"><a href="#constexpr" class="headerlink" title="constexpr"></a><strong>constexpr</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">constexpr int Fib(int n)</span><br><span class="line">&#123;</span><br><span class="line">	return n&lt;=2 ? 1 : Fib(n-1)+Fib(n-2);</span><br><span class="line">&#125;</span><br><span class="line">cout &lt;&lt; Fib(15); //compile time</span><br><span class="line">int a = 15;</span><br><span class="line">cout &lt;&lt; Fib(a); //runtime</span><br></pre></td></tr></table></figure>
<p>常量表达式，C++会自动在编译期替换能够解析的常量表达式。</p>
<h1 id="using"><a href="#using" class="headerlink" title="using"></a><strong>using</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">typedef int MyInt;</span><br><span class="line">using MyInt = int;</span><br><span class="line">template&lt; class T &gt; using MyType = AnotherType&lt; T, MyAllocatorType &gt;;</span><br></pre></td></tr></table></figure>
<p>C++11新增的用法，这个几乎跟typedef是等价的，但是要注意的是定义一个模板类别名的时候，只能使用using。</p>
<h1 id="nullptr"><a href="#nullptr" class="headerlink" title="nullptr"></a><strong>nullptr</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">void foo(char*);</span><br><span class="line">void foo(int);</span><br><span class="line">foo(nullptr); </span><br><span class="line">foo(NULL);</span><br></pre></td></tr></table></figure>
<p>nullptr并不是一个指针，而是一个类型，而NULL只是一个为0的宏定义，所以foo(NULL)会调用foo(int),而foo(nullptr)会调用foo(char *).</p>
<h1 id="enum-class"><a href="#enum-class" class="headerlink" title="enum class"></a><strong>enum class</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">enum class Alert &#123; green, yellow, red &#125;;</span><br><span class="line">enum class Color : int&#123; red, blue &#125;;</span><br></pre></td></tr></table></figure>
<p>C++11增强的enum的功能，C++11以前的enum是没用域名的，都是全局的，所以当两个enum类型含有同样的枚举类型的时候会报错，而C++11增加了class关键字，增加了域名，解决了这个问题。</p>
<h1 id="static-assert"><a href="#static-assert" class="headerlink" title="static_assert"></a><strong>static_assert</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static_assert(sizeof(v) == 4, “v must have size of 4 bytes&quot;);</span><br></pre></td></tr></table></figure>
<p>静态断言，在编译期检查出错误。</p>
<h1 id="raw-string-literals"><a href="#raw-string-literals" class="headerlink" title="raw string literals"></a><strong>raw string literals</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">u8&quot;I&apos;m a UTF-8 string.&quot; </span><br><span class="line">u&quot;This is a UTF-16 string.“</span><br><span class="line">U&quot;This is a UTF-32 string.“</span><br><span class="line">R&quot;(The String Data \ Stuff &quot; )&quot;</span><br><span class="line">u8R&quot;XXX(I&apos;m a &quot;raw UTF-8&quot; string.)XXX&quot; </span><br><span class="line">uR&quot;*(This is a &quot;raw UTF-16&quot; string.)*&quot; </span><br><span class="line">UR&quot;(This is a &quot;raw UTF-32&quot; string.)&quot;</span><br></pre></td></tr></table></figure>
<h1 id="delegating-constructor"><a href="#delegating-constructor" class="headerlink" title="delegating constructor"></a><strong>delegating constructor</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class A</span><br><span class="line">&#123;</span><br><span class="line">	int a;</span><br><span class="line">public:</span><br><span class="line">	A(int x)&#123;&#125;</span><br><span class="line">	A() : A(42)&#123; &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h1 id="in-class-member-initializers"><a href="#in-class-member-initializers" class="headerlink" title="in-class member initializers"></a><strong>in-class member initializers</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">class A</span><br><span class="line">&#123;</span><br><span class="line">	int a = 4;</span><br><span class="line">	string h = &quot;text1&quot;;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h1 id="variadic-templates"><a href="#variadic-templates" class="headerlink" title="variadic templates"></a><strong>variadic templates</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">template &lt;class ...T&gt;</span><br><span class="line">void f(T... args);</span><br><span class="line">f(“test”,42,’s’,12.f);</span><br></pre></td></tr></table></figure>
<h1 id="user-defined-literals"><a href="#user-defined-literals" class="headerlink" title="user defined literals"></a><strong>user defined literals</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int operator &quot;&quot; _km(int val)</span><br><span class="line">&#123;</span><br><span class="line">	return (int)val/1000;</span><br><span class="line">&#125;</span><br><span class="line">Speed v = 100_km/1_h;</span><br></pre></td></tr></table></figure>
<h1 id="override-and-final"><a href="#override-and-final" class="headerlink" title="override and final"></a><strong>override and final</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">struct Base&#123;</span><br><span class="line">	virtual void some_func(float);</span><br><span class="line">&#125;;</span><br><span class="line">struct Derived : Base&#123;</span><br><span class="line">	void some_func(int) override;//error</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">struct Base1 final &#123;&#125;;</span><br><span class="line">struct Derived1 : Base1&#123;//error</span><br><span class="line">&#125;;</span><br><span class="line">struct Base2&#123;</span><br><span class="line">	virtual void f() final;</span><br><span class="line">&#125;;</span><br><span class="line">struct Derived2 : Base2 &#123;</span><br><span class="line">	void f(); //error</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h1 id="explicit-conversion-operators"><a href="#explicit-conversion-operators" class="headerlink" title="explicit conversion operators"></a><strong>explicit conversion operators</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">struct A &#123;</span><br><span class="line">	A(int) &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line">struct B &#123;</span><br><span class="line">	int m;</span><br><span class="line">	B(int x) : m(x) &#123;&#125;</span><br><span class="line">	explicit operator A()&#123; </span><br><span class="line">		return A(m); </span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line">void f(A)&#123;&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">	B b(1);</span><br><span class="line">	A a = b; //error</span><br><span class="line">	f(b); //error</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="default-and-delete"><a href="#default-and-delete" class="headerlink" title="default and delete"></a><strong>default and delete</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">class A&#123;</span><br><span class="line">	A&amp; operator=(A) = delete; // C++03 use private </span><br><span class="line">	A(int a);</span><br><span class="line">	A() = default;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h1 id="decltype-and-declval"><a href="#decltype-and-declval" class="headerlink" title="decltype and declval"></a><strong>decltype and declval</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int i = 4;</span><br><span class="line">decltype (i = 5) j;      //int</span><br><span class="line">int a[5];</span><br><span class="line">decltype(a[2]) var;      //int &amp;</span><br><span class="line">declval&lt;T&gt;()  declval get an  rvalue references of class T</span><br></pre></td></tr></table></figure>
<h1 id="suffix-return-syntax"><a href="#suffix-return-syntax" class="headerlink" title="suffix return syntax"></a><strong>suffix return syntax</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">template&lt;class T, class U&gt;</span><br><span class="line">auto add(T x, U y) -&gt; decltype(x+y)&#123;</span><br><span class="line">	return x+y;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>主要用在模板和lambda表达式上。</p>
<h1 id="range-based-for-loop"><a href="#range-based-for-loop" class="headerlink" title="range based for loop"></a><strong>range based for loop</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">vector&lt;int&gt; v;</span><br><span class="line">int total = 0;</span><br><span class="line">for( auto d : v )</span><br><span class="line">	total += d;</span><br><span class="line"></span><br><span class="line">sort( begin(v), end(v) );</span><br></pre></td></tr></table></figure>
<h1 id="std-funcion-and-std-bind"><a href="#std-funcion-and-std-bind" class="headerlink" title="std::funcion and std::bind"></a><strong>std::funcion and std::bind</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">struct Foo&#123;</span><br><span class="line">	void f(int i)&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line">function&lt;void(Foo&amp;, int)&gt; fmem = mem_fn(&amp;Foo::f);</span><br><span class="line">Foo foo;</span><br><span class="line">function&lt;void(int)&gt; fmember = bind(&amp;Foo::f, foo, _1);</span><br><span class="line">fmember(42);</span><br></pre></td></tr></table></figure>
<p>类似C#的委托。</p>
<h1 id="tuple-and-tie"><a href="#tuple-and-tie" class="headerlink" title="tuple and tie"></a><strong>tuple and tie</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">int a = 5;</span><br><span class="line">int b = 6;</span><br><span class="line">auto tu= make_tuple(a, b);</span><br><span class="line">auto x = get&lt;1&gt;(tu);</span><br><span class="line">tie(b, a) = tu;</span><br></pre></td></tr></table></figure>
<p>话说tuple可是个好东西，能快速构建具有关系的数据结构。而tie则是拆解数据结构的。</p>
<h1 id="STL"><a href="#STL" class="headerlink" title="STL"></a><strong>STL</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">std::regex;</span><br><span class="line">std::chrono;</span><br><span class="line">std::ratio;</span><br><span class="line"></span><br><span class="line">New algorithms:</span><br><span class="line">std::all_of, std::none_of, std::any_of,</span><br><span class="line">std::find_if_not, std::copy_if, std::copy_n,</span><br><span class="line">std::move, std::move_n, std::move_backward,</span><br><span class="line">std::shuffle, </span><br><span class="line">std::random_shuffle,</span><br><span class="line">std::is_partitioned, std::partition_copy,</span><br><span class="line">std::partition_point, std::is_sorted,</span><br><span class="line">std::is_sorted_until, std::is_heap_until,</span><br><span class="line">std::min_max, std::minmax_element,</span><br><span class="line">std::is_permutation, std::iota</span><br><span class="line"></span><br><span class="line">String:</span><br><span class="line">stoi,stol,stoll,stoul,stoull,stof,stod,stold,to_string,to_wstring</span><br><span class="line"></span><br><span class="line">Vector: //尽量使用下面两个函数</span><br><span class="line">shrink_to_fit, emplace_back</span><br><span class="line"></span><br><span class="line">vector&lt;Some_type&gt; v;</span><br><span class="line">v.push_back(Some_type(1,2,3)); //构造对象再赋值或者移动到新对象</span><br><span class="line">v.emplace_back(1,2,3);  //直接在对象上初始化</span><br></pre></td></tr></table></figure>
<h1 id="type-traits"><a href="#type-traits" class="headerlink" title="type traits"></a><strong>type traits</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;type_traits&gt;</span><br><span class="line">#include &lt;iostream&gt;</span><br><span class="line">using namespace std;</span><br><span class="line">struct A &#123; &#125;;</span><br><span class="line">struct B &#123; </span><br><span class="line">	virtual void f()&#123;&#125; </span><br><span class="line">&#125;;</span><br><span class="line">struct C : B &#123;&#125;;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	cout &lt;&lt; &quot;int:&quot; &lt;&lt; has_virtual_destructor&lt;int&gt;::value &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; &quot;int:&quot;&lt;&lt; is_polymorphic&lt;int&gt;::value &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; &quot;A: &quot; &lt;&lt; is_polymorphic&lt;A&gt;::value &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; &quot;B: &quot; &lt;&lt; is_polymorphic&lt;B&gt;::value &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; &quot;C: &quot; &lt;&lt; is_polymorphic&lt;C&gt;::value &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line">int:0</span><br><span class="line">int:0</span><br><span class="line">A: 0</span><br><span class="line">B: 1</span><br><span class="line">C: 1</span><br><span class="line">类似的函数有：</span><br><span class="line">1: has_trivial_default_constructor</span><br><span class="line">2: has_trivial_copy_constructor </span><br><span class="line">3: has_trivial_assignment_operator</span><br><span class="line">4: has_trivial_destructor </span><br><span class="line">5: is_POD_type</span><br><span class="line">……</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">关于traits,给一个简单的例子，你就能明白有什么用法了:</span><br><span class="line">class CComplexObject&#123;</span><br><span class="line">public:</span><br><span class="line">	void clone() &#123; </span><br><span class="line">		cout &lt;&lt; &quot;in clone&quot; &lt;&lt; endl; </span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line">template &lt;typename T, bool isClonable&gt;</span><br><span class="line">class XContainer&#123;</span><br><span class="line">public:</span><br><span class="line">	void clone(T* pObj)&#123;</span><br><span class="line">		Traits&lt;isClonable&gt;().clone(pObj);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	template &lt;bool flag&gt;</span><br><span class="line">	class Traits  &#123;  &#125;;</span><br><span class="line"></span><br><span class="line">	template &lt;&gt;  </span><br><span class="line">	class Traits&lt;true&gt;&#123;</span><br><span class="line">	public:</span><br><span class="line">		void clone(T* pObj)&#123;</span><br><span class="line">			pObj-&gt;clone(); </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line"></span><br><span class="line">	template &lt;&gt;  </span><br><span class="line">	class Traits&lt;false&gt;&#123;</span><br><span class="line">	public:</span><br><span class="line">		void clone(T* pObj)&#123;</span><br><span class="line">			cout &lt;&lt; &quot;cloning non Clonable type&quot; &lt;&lt; endl;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;;</span><br><span class="line">void main()&#123;</span><br><span class="line">	int* p1 = 0;</span><br><span class="line">	CComplexObject* p2 = 0;</span><br><span class="line">	XContainer&lt;int, false&gt; n1;</span><br><span class="line">	XContainer&lt;CComplexObject, true&gt; n2;</span><br><span class="line">	n1.clone(p1);</span><br><span class="line">	n2.clone(p2);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="lambda-closures"><a href="#lambda-closures" class="headerlink" title="lambda/closures"></a><strong>lambda/closures</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">void test()&#123;</span><br><span class="line">	int x = 4;</span><br><span class="line">	int y = 5;</span><br><span class="line">	[&amp;]()&#123;x = 2;y = 2;&#125;();            //test[x=2,y=2]; lambda[x=2,y=2]</span><br><span class="line">	[=]() mutable&#123;x = 3;y = 5;&#125;();    //test[x=2,y=2]; lambda[x=3,y=5]</span><br><span class="line">	[=,&amp;x]() mutable&#123;x = 7;y = 9;&#125;(); //test[x=2,y=2]; lambda[x=7,y=9]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void test()&#123;</span><br><span class="line">	int x = 4;</span><br><span class="line">	int y = 5;</span><br><span class="line">	auto z = [=]() mutable&#123;x = 3;++y; int w = x + y; return w; &#125;;</span><br><span class="line">	z();   //test[x=4,y=5]; lambda[x=3,y=6,w=9]</span><br><span class="line">	z();   //test[x=4,y=5]; lambda[x=3,y=7,w=10]</span><br><span class="line">	z();   //test[x=4,y=5]; lambda[x=3,y=8,w=11]</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">递归lambda：</span><br><span class="line">function&lt;int(int)&gt; f = [&amp;f](int n)</span><br><span class="line">&#123;</span><br><span class="line">	return n &lt;= 1 ? 1 : n * f(n - 1);</span><br><span class="line">&#125;;</span><br><span class="line">int x = f(4); //x = 24</span><br></pre></td></tr></table></figure>
<p>lambda的定义方式[变量捕获方式](参数)-&gt;返回类型{函数体}()，其中以 = 捕获的变量，在lambda中默认是不允许修改的，必须添加 mutable 关键字才可以修改。</p>
<h1 id="uniform-and-general-initialization"><a href="#uniform-and-general-initialization" class="headerlink" title="uniform and general initialization"></a><strong>uniform and general initialization</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">vector&lt;int&gt; v = &#123; 1, 2, 3, 4, 5 &#125;;</span><br><span class="line">map&lt;int, string&gt; labels &#123;</span><br><span class="line">	&#123; 1 , &quot;Open&quot; &#125;,</span><br><span class="line">	&#123; 2 , &quot;Close&quot; &#125;,</span><br><span class="line">	&#123; 3 , &quot;Reboot&quot; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">struct Vector3&#123;</span><br><span class="line">	float x,y,z;</span><br><span class="line">	Vector3(float _x, float _y, float _z): x(_x), y(_y), z(_z)&#123;&#125;</span><br><span class="line">	//I don’t see ctor with std::initializer_list!</span><br><span class="line">&#125;;</span><br><span class="line">Vector3 normalize(const Vector3&amp; v)&#123;</span><br><span class="line">	float inv_len = 1.f/ length(v);</span><br><span class="line">	return &#123;v.x*inv_len, v.y*inv_len, v.z*inv_len&#125;;</span><br><span class="line">&#125;</span><br><span class="line">Vector3 x = normalize(&#123;2,5,9&#125;);</span><br><span class="line">Vector3 y&#123;4,2,1&#125;;</span><br><span class="line"></span><br><span class="line">The answer is:</span><br><span class="line">now you can use &#123;&#125; instead of ()</span><br><span class="line">But what about following case:</span><br><span class="line">struct T &#123;</span><br><span class="line">	T(int,int);</span><br><span class="line">	T(initializer_list&lt;int&gt;);</span><br><span class="line">&#125;;</span><br><span class="line">T foo &#123;10,20&#125;; // calls initializer_list ctor</span><br><span class="line">T bar (10,20); // calls first constructor</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Initializer-list constructors take precedence over other constructors when the initializer-list constructor syntax is used!</span><br><span class="line">So, be careful! Consider following example:</span><br><span class="line">vector&lt;int&gt; v(5); // v contains five elements &#123;0,0,0,0,0&#125;</span><br><span class="line">vector&lt;int&gt; v&#123;5&#125;; // v contains one element &#123;5&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&quot;The most vexing parse&quot; problem:</span><br><span class="line">struct B&#123;</span><br><span class="line">	B()&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line">struct A&#123;</span><br><span class="line">	A(B)&#123;&#125;</span><br><span class="line">	void f()&#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line">int main()&#123;</span><br><span class="line">	A a(B()); //this is function declaration!</span><br><span class="line">	a.f(); //compile error!</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">the type of variable a is:</span><br><span class="line">A a(B (*)())</span><br><span class="line"></span><br><span class="line">so, wo should write like this:</span><br><span class="line">int main()&#123;</span><br><span class="line">	A a&#123;B()&#125;; //calls B ctor, then A ctor. </span><br><span class="line">	//or A a((B()));</span><br><span class="line">	a.f(); //calls A::f</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="memory-management"><a href="#memory-management" class="headerlink" title="memory management"></a><strong>memory management</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">unique_ptr&lt;int&gt; p1(new int(42));</span><br><span class="line">unique_ptr&lt;int&gt; p2 = p1;              //Compile error. Only “move” operation is possible.</span><br><span class="line">unique_ptr&lt;int&gt; p3 = move(p1); //Transfers ownership. p3 now owns the memory and p1 is nullptr.</span><br><span class="line">p3.reset();         //Deletes the memory.</span><br><span class="line">p1.reset();         //Does nothing.</span><br><span class="line"></span><br><span class="line">void test()&#123;</span><br><span class="line">	shared_ptr&lt;int&gt; p( new int(42) ); 				// ref count = 1, weak count = 0</span><br><span class="line">	&#123;</span><br><span class="line">		shared_ptr&lt;int&gt; x = p; 						//ref count = 2, weak count = 0</span><br><span class="line">		&#123;</span><br><span class="line">			shared_ptr&lt;int&gt; y = p;					//ref count = 3, weak count = 0</span><br><span class="line">		&#125;           								//ref count = 2, weak count = 0</span><br><span class="line">	&#125;                                               //ref count = 1, weak count = 0</span><br><span class="line">	// use weak_ptr to break reference-count cycles</span><br><span class="line">	weak_ptr&lt;int&gt; wp = p;                 	//ref count = 1, weak count = 1 – note ref count is still 1</span><br><span class="line">	shared_ptr&lt;int&gt; ap = wp.lock();      	//ref count = 2, weak count = 1</span><br><span class="line">	&#123;</span><br><span class="line">		shared_ptr&lt;int&gt; y = ap;         	//ref count = 3, weak count = 1</span><br><span class="line">	&#125;                                    	//ref count = 2, weak count = 1</span><br><span class="line">&#125;                                          	//ap dtor:ref count = 1, weak count = 1</span><br><span class="line">                                            //wp dtor:ref count = 1, weak count = 0</span><br><span class="line">                                            //p dtor: ref count = 0, weak count = 0 - destroy p!</span><br></pre></td></tr></table></figure>
<p>这里，shared_ptr和unique_ptr，其实没什么可讲的，知道怎么用的就差不多了，需要注意的点有，引用计数变量是放在堆上的，在配合std::move使用的时候要小心。</p>
<h1 id="rvalue-reference-move-semantics"><a href="#rvalue-reference-move-semantics" class="headerlink" title="rvalue reference(move semantics)"></a><strong>rvalue reference(move semantics)</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">			   expression</span><br><span class="line">		glvalue			 rvalue</span><br><span class="line">lvalue			xvalue		  prvalue</span><br><span class="line"></span><br><span class="line">表达式分为泛左值和右值，泛左值则分为左值和临终值，右值分为右值引用和纯右值，临终值和右值引用都为xvalue，表达式的隐式转换只有从 glvalue -&gt; prvalue</span><br><span class="line">int a[5];</span><br><span class="line">int &amp;getInt(int i)&#123;</span><br><span class="line">	return a[i];</span><br><span class="line">&#125;</span><br><span class="line">int foo()&#123; return 1;&#125;</span><br><span class="line">int main()&#123;</span><br><span class="line">	int x = 2;</span><br><span class="line">	getInt(x);              //lvalue-&gt;prvalue</span><br><span class="line">	getInt(foo());          //xvalue-&gt;prvalue</span><br><span class="line">	getInt(3) = 5;          </span><br><span class="line">	string a, b;</span><br><span class="line">	a + b = “wow”;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="move-constructor-forward"><a href="#move-constructor-forward" class="headerlink" title="move constructor/ forward"></a><strong>move constructor/ forward</strong></h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">template &lt;typename T&gt;</span><br><span class="line">class Buffer </span><br><span class="line">&#123;</span><br><span class="line">	std::string _name;</span><br><span class="line">	size_t _size;</span><br><span class="line">	std::unique_ptr&lt;T[]&gt; _buffer;</span><br><span class="line"></span><br><span class="line">public:</span><br><span class="line">	// move constructor</span><br><span class="line">	Buffer(Buffer&amp;&amp; temp):</span><br><span class="line">		_name(std::move(temp._name)),</span><br><span class="line">		_size(temp._size),</span><br><span class="line">		_buffer(std::move(temp._buffer))</span><br><span class="line">	&#123;</span><br><span class="line">		temp._buffer = nullptr;</span><br><span class="line">		temp._size = 0;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">移动构造函数实现交换：</span><br><span class="line">template&lt;typename T&gt;</span><br><span class="line">void swap(T&amp; a, T&amp; b)</span><br><span class="line">&#123;</span><br><span class="line">	T t(std::move(a));</span><br><span class="line">	a = std::move(b);</span><br><span class="line">	b = std::move(t);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">关于forward：</span><br><span class="line">#include&lt;iostream&gt;  </span><br><span class="line">using namespace std;</span><br><span class="line">struct X &#123;&#125;;</span><br><span class="line">void inner(const X&amp;)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;inner(const X&amp;)&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">void inner(X&amp;&amp;)&#123;</span><br><span class="line">	cout &lt;&lt; &quot;inner(X&amp;&amp;)&quot; &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line">template&lt;typename T&gt;</span><br><span class="line">void outer(T&amp;&amp; t)&#123;</span><br><span class="line">	inner(t); </span><br><span class="line">	inner(forward&lt;T&gt;(t));</span><br><span class="line">&#125;</span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	X a;</span><br><span class="line">	outer(a);		//X&amp; + &amp;&amp; =&gt; X&amp; </span><br><span class="line">	//void outer(X&amp; t)&#123; inner(t); inner(forward&lt;X&amp;&gt;(t)); &#125;</span><br><span class="line">	outer(X());		//X&amp;&amp; + &amp;&amp; =&gt; X&amp;&amp; </span><br><span class="line">	//void outer(X&amp;&amp; t)&#123; inner(t); inner(forward&lt;X&amp;&amp;&gt;(t); &#125;</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">C++不允许定义引用的引用，所以在模板类解析的时候碰到引用的引用会进行折叠，具体折叠规则如下：</span><br><span class="line">X&amp; + &amp; =&gt; X&amp;</span><br><span class="line">X&amp;&amp; + &amp; =&gt; X&amp;</span><br><span class="line">X&amp; + &amp;&amp; =&gt; X&amp;</span><br><span class="line">X&amp;&amp; + &amp;&amp; =&gt; X&amp;&amp;</span><br></pre></td></tr></table></figure>
<p>真像是这样的，std::move在任何情况下都返回对象的右值引用，而std::forward只有在特定的情况下才返回类型的右值引用，当上层函数传入的是左值时返回左值，上层函数传入的右值时返回右值，所以std::forward是为了模板而生的。<br>具体可以参考std::move和std::forward的源码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">template &lt;typename T&gt; </span><br><span class="line">decltype(auto) move(T&amp;&amp; param) &#123; </span><br><span class="line">	using return_type = std::remove_reference&lt;T&gt;::type&amp;&amp;; </span><br><span class="line">	return static_cast&lt;return_type&gt;(param); </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">template &lt;typename T&gt; </span><br><span class="line">T&amp;&amp; forward(typename remove_reference&lt;T&gt;::type&amp; param) &#123;</span><br><span class="line">	return static_cast&lt;T&amp;&amp;&gt;(param); </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="thread-and-memory-model"><a href="#thread-and-memory-model" class="headerlink" title="thread and memory model"></a><strong>thread and memory model</strong></h1><p>其实这块没什么好讲的，主要点如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Threading support:</span><br><span class="line">thread, mutex, condition variable, async, future, promise, package task</span><br><span class="line"></span><br><span class="line">Memory model:</span><br><span class="line">atomic, fence</span><br></pre></td></tr></table></figure>
<p>下面主要介绍一下几个特性的用法：</p>
<ul>
<li>async</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt; </span><br><span class="line">#include &lt;future&gt; </span><br><span class="line">#include &lt;chrono&gt; </span><br><span class="line">using namespace std; </span><br><span class="line"></span><br><span class="line">int square(int x) &#123; return x * x; &#125; </span><br><span class="line">int main() &#123; </span><br><span class="line">	auto a = async(&amp;square, 10);</span><br><span class="line">	int v = a.get(); </span><br><span class="line">	cout &lt;&lt; &quot;The thread returned &quot; &lt;&lt; v &lt;&lt; endl; </span><br><span class="line">	return 0; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>promise/future</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;      </span><br><span class="line">#include &lt;functional&gt;    </span><br><span class="line">#include &lt;thread&gt;         </span><br><span class="line">#include &lt;future&gt;       </span><br><span class="line"></span><br><span class="line">void print_int(std::future&lt;int&gt;&amp; fut) &#123;</span><br><span class="line">	int x = fut.get(); </span><br><span class="line">	std::cout &lt;&lt; &quot;value: &quot; &lt;&lt; x &lt;&lt; &apos;\n&apos;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">	std::promise&lt;int&gt; prom;</span><br><span class="line">	std::future&lt;int&gt; fut = prom.get_future();</span><br><span class="line">	std::thread t(print_int, std::ref(fut));</span><br><span class="line">	prom.set_value(10);</span><br><span class="line">	t.join();</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line">std::ref 和&amp;的区别：</span><br><span class="line">   std::ref只是尝试模拟引用传递，并不能真正变成引用，在非模板情况下，std::ref根本没法实现引用传递，只有模板自动推导类型时，ref能用包装类型reference_wrapper来代替原本会被识别的值类型，而reference_wrapper能隐式转换为被引用的值的引用类型。</span><br></pre></td></tr></table></figure>
<ul>
<li>packaged_task</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt; </span><br><span class="line">#include &lt;future&gt;         </span><br><span class="line">#include &lt;chrono&gt;</span><br><span class="line">#include &lt;thread&gt;</span><br><span class="line">int countdown (int from, int to) &#123;</span><br><span class="line">	for (int i=from; i!=to; --i) &#123;</span><br><span class="line">		std::cout &lt;&lt; i &lt;&lt; &apos;\n&apos;;</span><br><span class="line">		std::this_thread::sleep_for(std::chrono::seconds(1));</span><br><span class="line">	&#125;</span><br><span class="line">	std::cout &lt;&lt; &quot;Finished!\n&quot;;</span><br><span class="line">	return from - to;</span><br><span class="line">&#125;</span><br><span class="line">int main ()</span><br><span class="line">&#123;</span><br><span class="line">	std::packaged_task&lt;int(int,int)&gt; task(countdown); </span><br><span class="line">	std::future&lt;int&gt; ret = task.get_future();</span><br><span class="line">	std::thread th(std::move(task), 10, 0);</span><br><span class="line">	int value = ret.get();</span><br><span class="line">	std::cout &lt;&lt; &quot;The countdown lasted for &quot; &lt;&lt; value &lt;&lt; &quot; seconds.\n&quot;;</span><br><span class="line">	th.join();</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>到此也就差不多了，还有一些新特性，并不常用，也还有一些旧的标准被废弃的，这里就不介绍了，感兴趣的可以自己查查。</p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/C-11/" rel="tag"># C++11</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/08/10/travel/张家界/" rel="next" title="张家界~2015.8.8">
                <i class="fa fa-chevron-left"></i> 张家界~2015.8.8
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/08/24/tech/Ubuntu14.04 + Matlab2014a + caffe + cuda + cudnn环境搭建/" rel="prev" title="Ubuntu14.04 + Matlab2014a + caffe + cuda + cudnn环境搭建">
                Ubuntu14.04 + Matlab2014a + caffe + cuda + cudnn环境搭建 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/logo1.jpg" alt="方林">
            
              <p class="site-author-name" itemprop="name">方林</p>
              <div class="site-description motion-element" itemprop="description">愿你出走半生，归来仍是少年</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">27</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">3</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">17</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/yourname" title="GitHub &rarr; https://github.com/yourname" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="mailto:sean_ps@163.com" title="E-Mail &rarr; mailto:sean_ps@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            </div>
          

          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.luoo.net" title="http://www.luoo.net" rel="noopener" target="_blank">落网</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#新特性列表"><span class="nav-number">1.</span> <span class="nav-text">新特性列表</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#auto"><span class="nav-number">2.</span> <span class="nav-text">auto</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#prevention-of-narrowing"><span class="nav-number">3.</span> <span class="nav-text">prevention of narrowing</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#constexpr"><span class="nav-number">4.</span> <span class="nav-text">constexpr</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#using"><span class="nav-number">5.</span> <span class="nav-text">using</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#nullptr"><span class="nav-number">6.</span> <span class="nav-text">nullptr</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#enum-class"><span class="nav-number">7.</span> <span class="nav-text">enum class</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#static-assert"><span class="nav-number">8.</span> <span class="nav-text">static_assert</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#raw-string-literals"><span class="nav-number">9.</span> <span class="nav-text">raw string literals</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#delegating-constructor"><span class="nav-number">10.</span> <span class="nav-text">delegating constructor</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#in-class-member-initializers"><span class="nav-number">11.</span> <span class="nav-text">in-class member initializers</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#variadic-templates"><span class="nav-number">12.</span> <span class="nav-text">variadic templates</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#user-defined-literals"><span class="nav-number">13.</span> <span class="nav-text">user defined literals</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#override-and-final"><span class="nav-number">14.</span> <span class="nav-text">override and final</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#explicit-conversion-operators"><span class="nav-number">15.</span> <span class="nav-text">explicit conversion operators</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#default-and-delete"><span class="nav-number">16.</span> <span class="nav-text">default and delete</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#decltype-and-declval"><span class="nav-number">17.</span> <span class="nav-text">decltype and declval</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#suffix-return-syntax"><span class="nav-number">18.</span> <span class="nav-text">suffix return syntax</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#range-based-for-loop"><span class="nav-number">19.</span> <span class="nav-text">range based for loop</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#std-funcion-and-std-bind"><span class="nav-number">20.</span> <span class="nav-text">std::funcion and std::bind</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#tuple-and-tie"><span class="nav-number">21.</span> <span class="nav-text">tuple and tie</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#STL"><span class="nav-number">22.</span> <span class="nav-text">STL</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#type-traits"><span class="nav-number">23.</span> <span class="nav-text">type traits</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#lambda-closures"><span class="nav-number">24.</span> <span class="nav-text">lambda/closures</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#uniform-and-general-initialization"><span class="nav-number">25.</span> <span class="nav-text">uniform and general initialization</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#memory-management"><span class="nav-number">26.</span> <span class="nav-text">memory management</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#rvalue-reference-move-semantics"><span class="nav-number">27.</span> <span class="nav-text">rvalue reference(move semantics)</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#move-constructor-forward"><span class="nav-number">28.</span> <span class="nav-text">move constructor/ forward</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#thread-and-memory-model"><span class="nav-number">29.</span> <span class="nav-text">thread and memory model</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">fanglin</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.0"></script>

  <script src="/js/motion.js?v=7.1.0"></script>



  
  


  <script src="/js/affix.js?v=7.1.0"></script>

  <script src="/js/schemes/pisces.js?v=7.1.0"></script>



  
  <script src="/js/scrollspy.js?v=7.1.0"></script>
<script src="/js/post-details.js?v=7.1.0"></script>



  


  <script src="/js/next-boot.js?v=7.1.0"></script>


  

  

  

  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: true,
    notify: false,
    appId: 'jjdDCzDO6jlN5yOgAbnVwdJ3-gzGzoHsz',
    appKey: 'jnjP7aorboCG7pJV6HvdMP15',
    placeholder: 'ヾﾉ≧∀≦)o来啊，快活啊!',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false,
    lang: 'zh-cn' || 'zh-cn'
  });
</script>




  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
